package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.ConsumptionQuotaDTO;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.dto.MarketingRulesDTO;
import com.quanquan.management.request.ConsumptionQuotaRequestBody;
import com.quanquan.management.service.ConsumptionQuotaService;
import com.quanquan.management.service.CouponService;
import com.quanquan.management.service.MarketingRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 *
 * @ClassName: ConsumptionQuotaController
 * @Author: xiaoqianh丶
 * @Date: 2019-06-12 09:47
 * @Version: V1.0
 */

@Controller
@RequestMapping("/consumption_quota")
public class ConsumptionQuotaController extends ManagerBaseController{

    private final CouponService couponService;
    private final ConsumptionQuotaService consumptionQuotaService;
    private final MarketingRulesService marketingRulesService;

    @Autowired
    public ConsumptionQuotaController(CouponService couponService, ConsumptionQuotaService consumptionQuotaService, MarketingRulesService marketingRulesService) {
        this.couponService = couponService;
        this.consumptionQuotaService = consumptionQuotaService;
        this.marketingRulesService = marketingRulesService;
    }


    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                        Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        List<MarketingRulesDTO> marketingRulesDTOS = marketingRulesService.list();
        model.addAttribute("switch",marketingRulesDTOS);
        ConsumptionQuotaRequestBody consumptionQuotaRequestBody = new ConsumptionQuotaRequestBody();
        consumptionQuotaRequestBody.setPage(page);
        consumptionQuotaRequestBody.setSize(pageSize);
        PageDTO<ConsumptionQuotaDTO> pageDTO = consumptionQuotaService.list(consumptionQuotaRequestBody);
        model.addAttribute("pageObj", pageDTO);
        return "consumptionQuota/list";
    }



    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(ConsumptionQuotaRequestBody requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        List<CouponDTO> parkingCouponDTOS = couponService.parkingCouponList();
        model.addAttribute("parkingCouponDTOS", parkingCouponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "consumptionQuota/form";
        }



        boolean add = consumptionQuotaService.addConsumptionQuota(requestBody);
        if (add) {
            return "redirect:/consumption_quota/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }


    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, ConsumptionQuotaRequestBody requestBody, Model model) {
        List<CouponDTO> couponDTOS = couponService.couponList();
        model.addAttribute("couponDTOS", couponDTOS);
        List<CouponDTO> parkingCouponDTOS = couponService.parkingCouponList();
        model.addAttribute("parkingCouponDTOS", parkingCouponDTOS);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            ConsumptionQuotaDTO byId = consumptionQuotaService.findById(id);
            model.addAttribute("editable",byId);
            return "consumptionQuota/form";
        }
        boolean update = consumptionQuotaService.update(requestBody);
        if (update) {
            return "redirect:/consumption_quota/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","修改失败");
            model.addAllAttributes(result);
            return null;
        }
    }
    @GetMapping("/delete")
    public String delete(@RequestParam String id,Model model) {
        boolean delete = consumptionQuotaService.delete(id);
        if (delete) {
            return "redirect:/consumption_quota/list";
        } else {
            model.addAttribute("code", -1);
        }
        return null;
    }
}
